package com.jarvis.springboot.starter.listener;

import cn.hutool.core.collection.CollUtil;
import com.google.inject.Guice;
import com.jarvis.core.Jarvis;
import com.jarvis.springboot.starter.config.JarvisProperty;
import com.jarvis.springboot.starter.guice.SpringModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xuxukang
 * @date 2019-07-04 17:11
 */
@Component
@Order(value = Ordered.HIGHEST_PRECEDENCE + 1)
public class JarvisStarterListener implements ApplicationListener<ContextRefreshedEvent> {

  @Autowired
  private List<SpringModule> springModules;

  @Autowired
  private JarvisProperty jarvisProperty;

  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    if (CollUtil.isNotEmpty(jarvisProperty.getDisableplugins())) {
      CollUtil.addAll(Jarvis.getDisabledPlugins(), jarvisProperty.getDisableplugins());
    }
    Guice.createInjector(springModules);
    Jarvis.INSTANCE.start();
  }

}
